package com.xoa.service.officesupplies;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.officesupplies.OfficeProductsMapper;
import com.xoa.dao.officesupplies.OfficeTranshistoryMapper;
import com.xoa.dao.officesupplies.OfficeTypeMapper;
import com.xoa.dao.sms.SmsBodyMapper;
import com.xoa.model.officesupplies.*;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.users.Users;
import com.xoa.service.sms.SmsService;
import com.xoa.util.AjaxJson;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import net.sf.json.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 创建作者:   高亚峰
 * 创建日期:   2017/10/8 13:37
 * 类介绍  :   办公用品申请记录
 * 构造参数:
 */
@Service
public class OfficeTransHistoryService {

    @Resource
    private OfficeTranshistoryMapper officeTranshistoryMapper;
    @Resource
    private OfficeTypeMapper officeTypeMapper;
    @Resource
    private OfficeProductsMapper  officeProductsMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private SmsService smsService;
    @Resource
    private SmsBodyMapper smsBodyMapper;
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/10/08 13:44
     * 类介绍  :   申请办公用品(flag分为自己申请1，代登记2，维护3)
     * 构造参数:
     */
    public ToJson<Object> insertObject(HttpServletRequest request,OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs,int flag){
        ToJson <Object> json =new ToJson<Object>(1,"err");
        try {
            OfficeProductsWithBLOBs temp=new OfficeProductsWithBLOBs();
            temp.setProId(officeTranshistoryWithBLOBs.getProId());
            temp.setOfficeProtype(String.valueOf(officeTranshistoryWithBLOBs.getTypeId()));
            temp.setDepositoryId(String.valueOf(officeTranshistoryWithBLOBs.getDepositoryId()));
            OfficeProductsWithBLOBs pro=officeProductsMapper.selByDepoAndTypeAndPro(temp);
            if(officeTranshistoryWithBLOBs.getTransQty()==null){
                officeTranshistoryWithBLOBs.setTransQty(1);
            }
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && !officeTranshistoryWithBLOBs.getTransFlag().equals("3")){//除采购入库外，其他类型需要判断其库存和最低警戒库存的数量
                if(pro!=null){
                    if((pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty())<pro.getProLowstock() && (pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty())<0){
                        json.setMsg("numNoEnough");
                        return json;
                    }
                }
            }else if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && officeTranshistoryWithBLOBs.getTransFlag().equals("3")){//采购入库，需要判断其最够警戒库存数
                if(pro!=null && pro.getProMaxstock()!=null && pro.getProMaxstock()!=0){
                    if((pro.getProStock()+officeTranshistoryWithBLOBs.getTransQty())>pro.getProMaxstock()){
                        json.setMsg("numMax");
                        return json;
                    }
                }
            }
            //从session中获取当前登录人的信息
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            if(flag==1){
                officeTranshistoryWithBLOBs.setBorrower(users.getUserId());
            }
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower())){
                if(officeTranshistoryWithBLOBs.getBorrower().contains(",")){
                    officeTranshistoryWithBLOBs.setBorrower(officeTranshistoryWithBLOBs.getBorrower().substring(0,officeTranshistoryWithBLOBs.getBorrower().length()-1));
                }
            }
            //申请人所属部门
            Users borrowUser=null;
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower())){
                borrowUser=usersMapper.getUserId(officeTranshistoryWithBLOBs.getBorrower());
            }

            if(borrowUser!=null){
                officeTranshistoryWithBLOBs.setDeptId(borrowUser.getDeptId());
            }
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getDeptManager())){
                if(officeTranshistoryWithBLOBs.getDeptManager().contains(",")){
                    officeTranshistoryWithBLOBs.setDeptManager(officeTranshistoryWithBLOBs.getDeptManager().substring(0,officeTranshistoryWithBLOBs.getDeptManager().length()-1));
                }
            }
            officeTranshistoryWithBLOBs.setOperator(users.getUserId());
            Date date =new Date();
            officeTranshistoryWithBLOBs.setTransDate(date);
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getDeptManager())){
                officeTranshistoryWithBLOBs.setTransState("01");
            }else{
                officeTranshistoryWithBLOBs.setTransState("02");
            }
            int i = officeTranshistoryMapper.insertSelective(officeTranshistoryWithBLOBs);
            if(pro!=null){
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && !officeTranshistoryWithBLOBs.getTransFlag().equals("3")) {//除采购入库外，其他类型需要将库存数量减少
                    pro.setProStock(pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty());
                }else if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && officeTranshistoryWithBLOBs.getTransFlag().equals("3")) {//采购入库，需要将库存数量进行添加
                    pro.setProStock(pro.getProStock()+officeTranshistoryWithBLOBs.getTransQty());
                }
            }
            i+=officeProductsMapper.updateByPrimaryKeyWithBLOBs(pro);
            //审批事务
            // 提醒
            //向sms_body插入提醒数据
            SmsBody smsBody = new SmsBody();
            smsBody.setFromId(users.getUserId());
            smsBody.setSmsType("24");
            smsBody.setContent("请查看办公审批信息");
            smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
            smsBody.setRemindUrl("/officetransHistory/NoApproval?status=1&transId="+ officeTranshistoryWithBLOBs.getTransId());
            if(officeTranshistoryWithBLOBs.getDeptManager()!=null) {
                smsService.saveSms(smsBody, officeTranshistoryWithBLOBs.getDeptManager().toString(), "1", "0", "", "", "");
            }
            if(i>0){
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/10/08 13:44
     * 类介绍  :   查询我的申请记录
     * 构造参数:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> getMyHistroy(HttpServletRequest request,int page,int pageSize,boolean useFlag){
        ToJson <OfficeTranshistoryWithBLOBs> json =new ToJson<OfficeTranshistoryWithBLOBs>(1,"err");
        //从session中获取当前登录人的信息
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
        OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs =new OfficeTranshistoryWithBLOBs();
        officeTranshistoryWithBLOBs.setBorrower(users.getUserId());
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map map=new HashMap();
        map.put("page",pageParams);
        map.put("officeTranshistoryWithBLOBs",officeTranshistoryWithBLOBs);
        try {
            List<OfficeTranshistoryWithBLOBs> myHistory = officeTranshistoryMapper.getMyHistory(map);
            for(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs1:myHistory){
                //办公用品名称
                Integer proId = officeTranshistoryWithBLOBs1.getProId();
                if(proId!=null&&proId!=0){
                    OfficeProductsWithBLOBs officeProductsWithBLOBs = officeProductsMapper.selectByPrimaryKey(proId);
                    if(officeProductsWithBLOBs!=null){
                        officeTranshistoryWithBLOBs1.setProName(officeProductsWithBLOBs.getProName());
                    }else{
                        officeTranshistoryWithBLOBs1.setProName("已删除办公用品");
                    }
                }
                //登记类型
                String transFlag = officeTranshistoryWithBLOBs1.getTransFlag();
                if(!StringUtils.checkNull(transFlag)){
                    if(transFlag.equals("1")){
                        officeTranshistoryWithBLOBs1.setTransFlagName("领用");
                    }else{
                        officeTranshistoryWithBLOBs1.setTransFlagName("借用");
                    }
                }
                //状态
                String transState = officeTranshistoryWithBLOBs1.getTransState();
                if(!StringUtils.checkNull(transState)){
                    if(transState.equals("01")){
                        officeTranshistoryWithBLOBs1.setTransStateName("待部门审批人审批");
                    }
                    else if(transState.equals("02")){
                        officeTranshistoryWithBLOBs1.setTransStateName("待库管理员审批");
                    }
                    else if(transState.equals("1")){
                        officeTranshistoryWithBLOBs1.setTransStateName("审批通过");
                    }
                    else if(transState.equals("21")){
                        officeTranshistoryWithBLOBs1.setTransStateName("部门审批驳回");
                    }
                    else{
                        officeTranshistoryWithBLOBs1.setTransStateName("库管员驳回");
                    }
                }
                //部门审批人
                String deptManager = officeTranshistoryWithBLOBs1.getDeptManager();
                Users byuserId = usersMapper.getUserId(deptManager);
                if(byuserId!=null){
                    officeTranshistoryWithBLOBs1.setDeptManagerName(byuserId.getUserName());
                }
            }
            json.setTotleNum(pageParams.getTotal());
            json.setObj(myHistory);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/10/08 13:44
     * 类介绍  :   删除申请记录
     * 构造参数:
     */

    public ToJson<Object> deleteObject(Integer transId){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            OfficeProductsWithBLOBs pro=null;
            OfficeTranshistoryWithBLOBs trans=officeTranshistoryMapper.selectByPrimaryKey(transId);
            if(!StringUtils.checkNull(trans.getTransState())&&trans.getTransState()!=""){
                if(trans.getTransState().equals("01") || trans.getTransState().equals("02")){
                    OfficeProductsWithBLOBs temp=new OfficeProductsWithBLOBs();
                    temp.setProId(trans.getProId());
                    temp.setOfficeProtype(String.valueOf(trans.getTypeId()));
                    temp.setDepositoryId(String.valueOf(trans.getDepositoryId()));
                    pro=officeProductsMapper.selByDepoAndTypeAndPro(temp);
                    if(pro!=null && trans.getTransFlag().equals("3")){ //采购入库
                        pro.setProStock(pro.getProStock()-trans.getTransQty());
                        officeProductsMapper.updateByPrimaryKeyWithBLOBs(pro);
                    }
                }
            }
            int i = officeTranshistoryMapper.deleteByPrimaryKey(transId);
            if(i>0){
                //排除采购入库，删除完申领物品新增库存
                if(pro!=null && !trans.getTransFlag().equals("3")){
                    pro.setProStock(pro.getProStock()+trans.getTransQty());
                    officeProductsMapper.updateByPrimaryKeyWithBLOBs(pro);
                }
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:  2017/10/08 13:44
     * 类介绍  :   编辑申请记录
     * 构造参数:
     */
    public ToJson<Object> editObject(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            OfficeTranshistoryWithBLOBs trans=officeTranshistoryMapper.selectByPrimaryKey(officeTranshistoryWithBLOBs.getTransId());//原来申请的数量

            OfficeProductsWithBLOBs temp=new OfficeProductsWithBLOBs();
            temp.setProId(trans.getProId());
            temp.setOfficeProtype(String.valueOf(trans.getTypeId()));
            temp.setDepositoryId(String.valueOf(trans.getDepositoryId()));
            if(officeTranshistoryWithBLOBs.getTransQty()==null){
                officeTranshistoryWithBLOBs.setTransQty(1);
            }


            OfficeProductsWithBLOBs pro=officeProductsMapper.selByDepoAndTypeAndPro(temp);//获取办公用品的库存数量

            //思路：（办公用品库存+原申请数量-先现申请数量）和库存的最低警戒以及库存数量相比较
            if(!StringUtils.checkNull(trans.getTransFlag()) && !trans.getTransFlag().equals("3")){//除采购入库外，其他类型需要判断其库存和最低警戒库存的数量
                if(pro!=null){
                    if((pro.getProStock()+trans.getTransQty()-officeTranshistoryWithBLOBs.getTransQty())<pro.getProLowstock() && (pro.getProStock()+trans.getTransQty()-officeTranshistoryWithBLOBs.getTransQty())<0){
                        json.setMsg("numNoEnough");
                        return json;
                    }
                }
            }else if(!StringUtils.checkNull(trans.getTransFlag()) && trans.getTransFlag().equals("3")){//采购入库，需要判断其最够警戒库存数
                if(pro!=null && pro.getProMaxstock()!=null && pro.getProMaxstock()!=0){
                    if((pro.getProStock()-trans.getTransQty()+officeTranshistoryWithBLOBs.getTransQty())>pro.getProMaxstock()){
                        json.setMsg("numMax");
                        return json;
                    }
                }
            }
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower())){
                if(officeTranshistoryWithBLOBs.getBorrower().contains(",")){
                    officeTranshistoryWithBLOBs.setBorrower(officeTranshistoryWithBLOBs.getBorrower().substring(0,officeTranshistoryWithBLOBs.getBorrower().length()-1));
                }
            }
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getDeptManager())){
                if(officeTranshistoryWithBLOBs.getDeptManager().contains(",")){
                    officeTranshistoryWithBLOBs.setDeptManager(officeTranshistoryWithBLOBs.getDeptManager().substring(0,officeTranshistoryWithBLOBs.getDeptManager().length()-1));
                }
            }
            int i = officeTranshistoryMapper.updateByPrimaryKeySelective(officeTranshistoryWithBLOBs);
            if(i>0){
                //申请数修改完修改库存
                if (pro!=null && pro.getProStock()!=null && officeTranshistoryWithBLOBs.getTransQty()!=null){
                    if (trans.getTransFlag().equals("3")){ //采购入库
                        pro.setProStock((pro.getProStock()+officeTranshistoryWithBLOBs.getTransQty())-trans.getTransQty()); //（原当前库存总数+修改的申请数）-原申请数=新库存
                    }else{
                        pro.setProStock((pro.getProStock()+trans.getTransQty())-officeTranshistoryWithBLOBs.getTransQty() ); //（原当前库存总数+原申请数）-修改的申请数=新库存
                    }
                    officeProductsMapper.updateByPrimaryKeySelective(pro);
                }
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:  2017/10/08 13:44
     * 类介绍  :   申请记录详情接口
     * 构造参数:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> getObjectById(Integer transId){
        ToJson<OfficeTranshistoryWithBLOBs> json =new ToJson<OfficeTranshistoryWithBLOBs>(1,"err");
        try {
            OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs = officeTranshistoryMapper.selectByPrimaryKey(transId);
            String borrower = officeTranshistoryWithBLOBs.getBorrower();
            Users byuserId = usersMapper.getUserId(borrower);
            if(byuserId!=null){
                officeTranshistoryWithBLOBs.setBorrowerName(byuserId.getUserName());
            }
            //部门审批人
            String deptManager = officeTranshistoryWithBLOBs.getDeptManager();
            Users byuserId1 = usersMapper.getUserId(deptManager);
            if(byuserId1!=null){
                officeTranshistoryWithBLOBs.setDeptManagerName(byuserId1.getUserName());
            }
            json.setMsg("ok");
            json.setFlag(0);
            json.setObject(officeTranshistoryWithBLOBs);
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月8日 下午15:47:00
     * 方法介绍:   根据当前登录人查询待批申请
     * 参数说明:   @param currentUser 当前登录人
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> selTranshistoryByState(HttpServletRequest request,int page,int pageSize,boolean useFlag,OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            Map map=new HashMap();
            PageParams pageParams=new PageParams();
            pageParams.setPageSize(pageSize);
            pageParams.setPage(page);
            pageParams.setUseFlag(useFlag);
            map.put("currentUser",users.getUserId());
            map.put("page",pageParams);
            map.put("officeTranshistoryWithBLOBs",officeTranshistoryWithBLOBs);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selTranshistoryByState(map);
            for (OfficeTranshistoryWithBLOBs transhistoryWithBLOBs:list){
                Users eduUser=usersMapper.getUserId(transhistoryWithBLOBs.getBorrower());
                if(eduUser!=null){
                    transhistoryWithBLOBs.setBorrowerName(eduUser.getUserName());
                }
            }
            json.setObj(list);
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selTranshistoryByState:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月8日 下午15:47:00
     * 方法介绍:   审批办公用品(批准：1，不批准：2)
     * 参数说明:   @param transhistory(transId,transState)
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> upTransHistoryState(String transState, String transIds, HttpServletRequest request){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        try{
            String[] transId = transIds.split(",");
            for (int i = 0; i < transId.length; i++) {
                OfficeTranshistoryWithBLOBs transhistory = new OfficeTranshistoryWithBLOBs();
                int anInt = Integer.parseInt(transId[i]);
                transhistory.setTransId(anInt);
                OfficeTranshistoryWithBLOBs temp=officeTranshistoryMapper.selectByPrimaryKey(anInt);
                String pd=temp.getTransState();
                if(("1").equals(transState)){//同意

                    if ((temp.getTransState().equals("02")) || (temp.getTransState().equals("01") && users.getUserPriv()==1)){ //超级管理员
                        transhistory.setTransState("1");

                        SmsBody smsBody = new SmsBody();
                        smsBody.setFromId(users.getUserId());

                        smsBody.setSmsType("24");
                        smsBody.setContent("您申领的办公用品已通过，请前往领取并确认");
                        smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
                        smsBody.setRemindUrl("/officetransHistory/goMyOfficeApply");
                        smsService.saveSms(smsBody,temp.getBorrower(),"1","0","","","");

                    }else if(temp.getTransState().equals("01")){  //原来为待部门审批人审批状态01，则修改为待库管理员审批状态02
                        transhistory.setTransState("02");
                        String manager= officeTranshistoryMapper.getManagerByTransId(anInt);
                        //审批事务
                        // 提醒
                        //向sms_body插入提醒数据
                        SmsBody smsBody = new SmsBody();
                        smsBody.setFromId(users.getUserId());

                        smsBody.setSmsType("24");
                        smsBody.setContent("请查看办公审批信息");
                        smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
                        smsBody.setRemindUrl("/officetransHistory/NoApproval?status=2&transId="+ transhistory.getTransId());
                        smsService.saveSms(smsBody,manager,"1","0","","","");
                    }
                }
                if(("2").equals(transState)){
                    if(temp.getTransState().equals("01")){  //原来为待部门审批人审批状态01，则修改为部门审批驳回21
                        transhistory.setTransState("21");
                    }
                    if(temp.getTransState().equals("02")){  //原来为待库管员审批状态02，则修改为库管员驳回22
                        transhistory.setTransState("22");
                    }

                    OfficeProductsWithBLOBs proTemp=new OfficeProductsWithBLOBs();
                    proTemp.setProId(temp.getProId());
                    proTemp.setOfficeProtype(String.valueOf(temp.getTypeId()));
                    proTemp.setDepositoryId(String.valueOf(temp.getDepositoryId()));
                    OfficeProductsWithBLOBs pro=officeProductsMapper.selByDepoAndTypeAndPro(proTemp);
                    pro.setProStock(pro.getProStock()+temp.getTransQty());
                    officeProductsMapper.updateByPrimaryKeyWithBLOBs(pro);
                }
                officeTranshistoryMapper.upTransHistoryState(transhistory);
                //消除审批事务提醒
                //判断是消除部门审批还是库管理事务提醒
                String remindUrl="";
                if("01".equals(pd)){
                    remindUrl= "/officetransHistory/NoApproval?status=1&transId="+transhistory.getTransId();
                }else if("02".equals(pd)){
                    remindUrl= "/officetransHistory/NoApproval?status=2&transId="+transhistory.getTransId();
                }
                String userId = users.getUserId();
                //根据路径查询出sms_body中的body_id
                SmsBody smsBody=smsBodyMapper.selectByRemindUrl(remindUrl);
                if(smsBody!=null){
                    smsService.updateRemindFlag(request,"0",smsBody.getBodyId()+",");
                }


            }
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selTranshistoryByState:"+e);
            e.printStackTrace();
        }
        return  json;
    }
    /**
     * 创建作者:   高亚峰
     *  创建日期:  2017/10/08 13:44
     * 类介绍  :   下拉框获取接口
     * 构造参数:
     */
    public AjaxJson getDownObjects(String typeDepository, String officeProductType){
        AjaxJson ajaxJson =new AjaxJson();
        if(!StringUtils.checkNull(typeDepository)){
            List<OfficeType> officeTypes = officeTypeMapper.selectDownObject(typeDepository);
            ajaxJson.setMsg("ok");
            ajaxJson.setFlag(true);
            ajaxJson.setSuccess(true);
            ajaxJson.setObj(officeTypes);
        }
        if(!StringUtils.checkNull(officeProductType)){
            List<OfficeProductsWithBLOBs> officeProductsWithBLOBs = officeProductsMapper.selectDownObject(officeProductType);
            ajaxJson.setMsg("ok");
            ajaxJson.setFlag(true);
            ajaxJson.setSuccess(true);
            ajaxJson.setObj(officeProductsWithBLOBs);
        }
        return ajaxJson;
    }

    public ToJson getDownObjectLikeBytype(String officeProductType,String proName){
        ToJson toJson=new ToJson();
        try{
            Map map=new HashMap();
            map.put("officeProductType",officeProductType);
            map.put("proName",proName);
            List<OfficeProductsWithBLOBs> officeProductsWithBLOBs = officeProductsMapper.getDownObjectLikeBytype(map);
            toJson.setObj(officeProductsWithBLOBs);
            toJson.setMsg("true");
            toJson.setFlag(0);
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月8日 下午15:47:00
     * 方法介绍:   根据条件进行办公用品发放查询（可分页）
     * 参数说明:   @param
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> selGrantByCond(OfficeTranshistoryWithBLOBs transhistory,HttpServletRequest request,Integer page, Integer pageSize, boolean useFlag){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            transhistory.setCurrentUser(users.getUserId());
            if(!StringUtils.checkNull(transhistory.getBorrower())){
                if(transhistory.getBorrower().contains(",")){
                    transhistory.setBorrower(transhistory.getBorrower().substring(0,transhistory.getBorrower().length()-1));
                }
            }
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("OfficeTranshistory",transhistory);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selGrantByCond(map);
            for(OfficeTranshistoryWithBLOBs temp:list){
                if(!StringUtils.checkNull(temp.getBorrower())){
                    if(usersMapper.getUserId(temp.getBorrower())!=null){
                        temp.setBorrowerName(usersMapper.getUserId(temp.getBorrower()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(temp.getGrantor())){
                    if(usersMapper.getUserId(temp.getGrantor())!=null){
                        temp.setGrantorName(usersMapper.getUserId(temp.getGrantor()).getUserName());
                    }
                }
            }
            json.setTotleNum(officeTranshistoryMapper.selGrantCountByCond(transhistory));
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selGrantByCond:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月8日 下午15:47:00
     * 方法介绍:   修改发放状态
     * 参数说明:   @param officeTranshistoryWithBLOBs
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs>  upGrantStatus (String transState, String transIds){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            int count = 0;
            String[] transId = transIds.split(",");
            for (int i = 0; i < transId.length; i++) {
                count += officeTranshistoryMapper.upGrantStatus(transState,transId[i]);
            }
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService upGrantStatus:"+e);
            e.printStackTrace();
        }
        return  json;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月9日 下午15:47:00
     * 方法介绍:   办公用品查询
     * 参数说明:   @param officeTranshistoryWithBLOBs
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> selTranshistoryByCond(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs, Integer output, HttpServletRequest request, HttpServletResponse response,Integer page,Integer pageSize,Boolean useFlag)  {
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        Map map=new HashMap();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            map.put("page",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{

            OutputStream  out=response.getOutputStream();
            if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower()) && officeTranshistoryWithBLOBs.getBorrower().contains(",")){
                officeTranshistoryWithBLOBs.setBorrower(officeTranshistoryWithBLOBs.getBorrower().substring(0,officeTranshistoryWithBLOBs.getBorrower().length()-1));
            }

            map.put("officeTranshistoryWithBLOBs",officeTranshistoryWithBLOBs);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selTranshistoryByCond(map);
            for (OfficeTranshistoryWithBLOBs transhistoryWithBLOBs:list){
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getBorrower())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower())!=null){
                        transhistoryWithBLOBs.setBorrowerName(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getOperator())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getOperator())!=null){
                        transhistoryWithBLOBs.setOperatorName(usersMapper.getUserId(transhistoryWithBLOBs.getOperator()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getGrantor())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor())!=null){
                        transhistoryWithBLOBs.setGrantorName(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor()).getUserName());
                    }
                }
                if(transhistoryWithBLOBs.getTransFlag().equals("1")){
                    transhistoryWithBLOBs.setTransFlagName("领用");
                }else  if(transhistoryWithBLOBs.getTransFlag().equals("2")){
                    transhistoryWithBLOBs.setTransFlagName("借用");
                }
                if(transhistoryWithBLOBs.getDeptId()!=null){
                    transhistoryWithBLOBs.setDeptName(departmentMapper.getDeptNameById(transhistoryWithBLOBs.getDeptId()));
                }
                transhistoryWithBLOBs.setGrantStatusName("未通过审核");
                if(transhistoryWithBLOBs.getTransState().equals("01")){
                    transhistoryWithBLOBs.setTransStateName("待部门审批人审批");
                }else  if(transhistoryWithBLOBs.getTransState().equals("02")){
                    transhistoryWithBLOBs.setTransStateName("待库管理员审批");
                }else  if(transhistoryWithBLOBs.getTransState().equals("1")){
                    transhistoryWithBLOBs.setTransStateName("审批通过");
                    if(transhistoryWithBLOBs.getGrantStatus().equals("0")){
                        transhistoryWithBLOBs.setGrantStatusName("等待发放");
                    }else if(transhistoryWithBLOBs.getGrantStatus().equals("1")){
                        transhistoryWithBLOBs.setGrantStatusName("已发放");
                    }
                }else  if(transhistoryWithBLOBs.getTransState().equals("21")){
                    transhistoryWithBLOBs.setTransStateName("部门审批驳回");
                }else  if(transhistoryWithBLOBs.getTransState().equals("22")){
                    transhistoryWithBLOBs.setTransStateName("库管员驳回");
                }
                //给定一个状态值flag，判断当前登录是发放员、审批员
                transhistoryWithBLOBs.setFlag(0);
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
                transhistoryWithBLOBs.setCurrentUser(users.getUserId());
                if(officeTranshistoryMapper.isApproval(transhistoryWithBLOBs)==1){
                    transhistoryWithBLOBs.setFlag(1);//可以进行审批
                }
                if(officeTranshistoryMapper.isGrant(transhistoryWithBLOBs)==1){
                    transhistoryWithBLOBs.setFlag(2);//可以进行发放
                }
            }
            if(output!=null && output==1) {//output==1,则进行导出操作
                HSSFWorkbook tableWork = ExcelUtil.makeExcelHead("办公用品申请记录表", 15);
                String[] secondTitles = {"办公用品名称", "办公用品库","办公用品类别","登记类型","申请人","所属部门","数量","价格","申请日期","审批状态","状态","备注"};
                HSSFWorkbook excelWork = ExcelUtil.makeSecondHead(tableWork, secondTitles);
                String[] beanProperty = {"proName","depositoryName","typeName","transFlagName","borrowerName","deptName","transQty","price","transDate","transStateName","grantStatusName","remark"};
                HSSFWorkbook workbook = ExcelUtil.exportExcelData(excelWork, list, beanProperty);
                response.setContentType("text/html;charset=UTF-8");


                String filename = "办公用品申请记录表.xls"; //考虑多语言
                filename = FileUtils.encodeDownloadFilename(filename,
                        request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition",
                        "attachment;filename=" + filename);
                workbook.write(out);
                out.flush();
            }
            json.setObj(list);
            PageParams pageParams=(PageParams) map.get("page");
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selTranshistoryByCond:"+e);
            e.printStackTrace();
        }
        return  json;
    }


    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月10日 下午15:47:00
     * 方法介绍:   查询今日维护办公用品列表（操作员是当前登录人）
     * 参数说明:   @param officeTranshistoryWithBLOBs
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> selMaintain(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs,HttpServletRequest request){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            Date date =new Date();
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            officeTranshistoryWithBLOBs.setCurrentUser(users.getUserId());
            officeTranshistoryWithBLOBs.setCurrentTime(format);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selMaintain(officeTranshistoryWithBLOBs);
            for (OfficeTranshistoryWithBLOBs transhistoryWithBLOBs:list){
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getBorrower())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower())!=null){
                        transhistoryWithBLOBs.setBorrowerName(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getOperator())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getOperator())!=null){
                        transhistoryWithBLOBs.setOperatorName(usersMapper.getUserId(transhistoryWithBLOBs.getOperator()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getGrantor())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor())!=null){
                        transhistoryWithBLOBs.setGrantorName(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor()).getUserName());
                    }
                }
            }
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selMaintain:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年10月10日 下午15:47:00
     * 方法介绍:   查询今日代登记办公用品列表（操作员是当前登录人）
     * 参数说明:   @param officeTranshistoryWithBLOBs
     * 返回值说明:
     */
    public ToJson<OfficeTranshistoryWithBLOBs> selInstead(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs,HttpServletRequest request){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            Date date =new Date();
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            officeTranshistoryWithBLOBs.setCurrentUser(users.getUserId());
            officeTranshistoryWithBLOBs.setCurrentTime(format);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selInstead(officeTranshistoryWithBLOBs);
            for (OfficeTranshistoryWithBLOBs transhistoryWithBLOBs:list){
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getBorrower())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower())!=null){
                        transhistoryWithBLOBs.setBorrowerName(usersMapper.getUserId(transhistoryWithBLOBs.getBorrower()).getUserName());
                    }
                }
                if(!StringUtils.checkNull(transhistoryWithBLOBs.getGrantor())){
                    if(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor())!=null){
                        transhistoryWithBLOBs.setGrantorName(usersMapper.getUserId(transhistoryWithBLOBs.getGrantor()).getUserName());
                    }
                }
            }
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selInstead:"+e);
            e.printStackTrace();
        }
        return  json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:  2017/10/08 13:44
     * 类介绍  :   编辑申请记录
     * 构造参数:
     */
    public ToJson<Object> returnObject(String transIds){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs =new OfficeTranshistoryWithBLOBs();
            String[] split = transIds.split(",");
            for(String s:split){
                //设置商品的id
                officeTranshistoryWithBLOBs.setTransId(Integer.valueOf(s));
                //设置归还时间
                Date date =new Date();
                officeTranshistoryWithBLOBs.setReturnDate(date);
                //改变归还状态
                officeTranshistoryWithBLOBs.setReturnStatus(1);
                //查询一下该商品原来的库存数量
                OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs1 = officeTranshistoryMapper.selectByPrimaryKey(Integer.valueOf(s));
                if(officeTranshistoryWithBLOBs1!=null){
                    Integer transQty = officeTranshistoryWithBLOBs1.getTransQty();
                    OfficeProductsWithBLOBs officeProductsWithBLOBs = officeProductsMapper.selectByPrimaryKey(officeTranshistoryWithBLOBs1.getProId());
                    Integer proStock = officeProductsWithBLOBs.getProStock();
                    int i = transQty + proStock;
                    OfficeProductsWithBLOBs officeProductsWithBLOBs1 =new OfficeProductsWithBLOBs();
                    officeProductsWithBLOBs.setProId(officeProductsWithBLOBs.getProId());
                    officeProductsWithBLOBs.setProStock(i);
                    int i1 = officeProductsMapper.updateByPrimaryKeySelective(officeProductsWithBLOBs);
                }

                officeTranshistoryMapper.updateByPrimaryKeySelective(officeTranshistoryWithBLOBs);
            }
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }

    //办公用品申请警告
    public ToJson getJingao(int proId,int transQty){
        ToJson toJson=new ToJson();
        try{
            //得到库存，最低，最高警戒线
            OfficeProducts officeProducts =officeProductsMapper.getJingao(proId);
            //判断数量是否超过
            if( transQty> officeProducts.getProStock()){
                //超过库存返回0
                toJson.setObject("0");
            }else if(transQty>officeProducts.getProLowstock()){
                //超过最低警戒线返回1
                toJson.setObject("1");
            }else if(transQty>officeProducts.getProMaxstock()){
                //超过最高警戒线返回2
                toJson.setObject("2");
            }else{
                //正常返回3
                toJson.setObject("3");
            }
            toJson.setMsg("true");
            toJson.setFlag(0);
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }

        return toJson;
    }
    public ToJson<OfficeTranshistoryWithBLOBs> getTranshistoryById(String borrower) {
        ToJson<OfficeTranshistoryWithBLOBs> json=new ToJson<OfficeTranshistoryWithBLOBs>();
        try{
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.getTranshistoryById(borrower);
            if(list.size()>0){
                json.setMsg("ok");
                json.setFlag(0);
                json.setObj(list);
            }else{
                json.setMsg("领用物品为空");
                json.setFlag(0);
                json.setObj(null);
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }
    public ToJson getApplayBatch(HttpServletRequest request,String jsonStr,String deptManager,String  remark,int flag){
        ToJson toJson=new ToJson();
        try{
            //得到申领的办公用品
            List<OfficeTranshistoryWithBLOBs> list= JSONArray.toList(JSONArray.fromObject(jsonStr),OfficeTranshistoryWithBLOBs.class);
            for(OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs:list){
                OfficeProductsWithBLOBs temp=new OfficeProductsWithBLOBs();
                temp.setProId(officeTranshistoryWithBLOBs.getProId());
                temp.setOfficeProtype(String.valueOf(officeTranshistoryWithBLOBs.getTypeId()));
                temp.setDepositoryId(String.valueOf(officeTranshistoryWithBLOBs.getDepositoryId()));
                OfficeProductsWithBLOBs pro=officeProductsMapper.selByDepoAndTypeAndPro(temp);
                if(officeTranshistoryWithBLOBs.getTransQty()==null){
                    officeTranshistoryWithBLOBs.setTransQty(1);
                }
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && !officeTranshistoryWithBLOBs.getTransFlag().equals("3")){//除采购入库外，其他类型需要判断其库存和最低警戒库存的数量
                    if(pro!=null){
                        if((pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty())<pro.getProLowstock() && (pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty())<0){
                            toJson.setMsg("numNoEnough");
                            return toJson;
                        }
                    }
                }else if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && officeTranshistoryWithBLOBs.getTransFlag().equals("3")){//采购入库，需要判断其最够警戒库存数
                    if(pro!=null && pro.getProMaxstock()!=null && pro.getProMaxstock()!=0){
                        if((pro.getProStock()+officeTranshistoryWithBLOBs.getTransQty())>pro.getProMaxstock()){
                            toJson.setMsg("numMax");
                            return toJson;
                        }
                    }
                }
                officeTranshistoryWithBLOBs.setTransFlag("1");
                officeTranshistoryWithBLOBs.setDeptManager(deptManager);
                officeTranshistoryWithBLOBs.setRemark(remark);
                //从session中获取当前登录人的信息
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
                if(flag==1){
                    officeTranshistoryWithBLOBs.setBorrower(users.getUserId());
                }
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower())){
                    if(officeTranshistoryWithBLOBs.getBorrower().contains(",")){
                        officeTranshistoryWithBLOBs.setBorrower(officeTranshistoryWithBLOBs.getBorrower().substring(0,officeTranshistoryWithBLOBs.getBorrower().length()-1));
                    }
                }
                //申请人所属部门
                Users borrowUser=null;
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getBorrower())){
                    borrowUser=usersMapper.getUserId(officeTranshistoryWithBLOBs.getBorrower());
                }

                if(borrowUser!=null){
                    officeTranshistoryWithBLOBs.setDeptId(borrowUser.getDeptId());
                }
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getDeptManager())){
                    if(officeTranshistoryWithBLOBs.getDeptManager().contains(",")){
                        officeTranshistoryWithBLOBs.setDeptManager(officeTranshistoryWithBLOBs.getDeptManager().substring(0,officeTranshistoryWithBLOBs.getDeptManager().length()-1));
                    }
                }
                officeTranshistoryWithBLOBs.setOperator(users.getUserId());
                Date date =new Date();
                officeTranshistoryWithBLOBs.setTransDate(date);
                if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getDeptManager())){
                    officeTranshistoryWithBLOBs.setTransState("01");
                }else{
                    officeTranshistoryWithBLOBs.setTransState("02");
                }

                int i = officeTranshistoryMapper.insertSelective(officeTranshistoryWithBLOBs);
                if(pro!=null){
                    if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && !officeTranshistoryWithBLOBs.getTransFlag().equals("3")) {//除采购入库外，其他类型需要将库存数量减少
                        pro.setProStock(pro.getProStock()-officeTranshistoryWithBLOBs.getTransQty());
                    }else if(!StringUtils.checkNull(officeTranshistoryWithBLOBs.getTransFlag()) && officeTranshistoryWithBLOBs.getTransFlag().equals("3")) {//采购入库，需要将库存数量进行添加
                        pro.setProStock(pro.getProStock()+officeTranshistoryWithBLOBs.getTransQty());
                    }
                }
                i+=officeProductsMapper.updateByPrimaryKeyWithBLOBs(pro);
                //审批事务
                // 提醒
                //向sms_body插入提醒数据
                SmsBody smsBody = new SmsBody();
                smsBody.setFromId(users.getUserId());
                smsBody.setSmsType("24");
                smsBody.setContent("请查看办公审批信息");
                smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
                smsBody.setRemindUrl("/officetransHistory/NoApproval?status=1&transId="+ officeTranshistoryWithBLOBs.getTransId());
                if(officeTranshistoryWithBLOBs.getDeptManager()!=null) {
                    smsService.saveSms(smsBody, officeTranshistoryWithBLOBs.getDeptManager().toString(), "1", "0", "", "", "");
                }
            }
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception  e){
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson queryOfficeProducts(Integer depositoryId, Integer typeId, Integer productsId, String beginDate, String endDate) {
        ToJson toJson = new ToJson();

        return null;
    }
    public ToJson<OfficeTranshistoryWithBLOBs> selShopList(HttpServletRequest request,Integer page, Integer limit, boolean useFlag){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            //分页
            PageParams pageParams = new PageParams();
            useFlag=true;
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selectShopList(map);
            for (OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs:list){
                officeTranshistoryWithBLOBs.setShoplistCount((officeTranshistoryWithBLOBs.getProLowstock()-officeTranshistoryWithBLOBs.getProStock())+"");
            }
            json.setTotleNum(officeTranshistoryMapper.selectShopListCount());
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("OfficeTransHistoryService selGrantByCond:"+e);
            e.printStackTrace();
        }
        return  json;
    }
    public ToJson<OfficeTranshistoryWithBLOBs> exportShopList(HttpServletRequest request,HttpServletResponse response){
        ToJson<OfficeTranshistoryWithBLOBs> json = new ToJson<OfficeTranshistoryWithBLOBs>(1, "error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            //分页
            Map<String, Object> map = new HashMap<String, Object>();
            List<OfficeTranshistoryWithBLOBs> list=officeTranshistoryMapper.selectShopList(map);
            for (OfficeTranshistoryWithBLOBs officeTranshistoryWithBLOBs:list){
                officeTranshistoryWithBLOBs.setShoplistCount((officeTranshistoryWithBLOBs.getProLowstock()-officeTranshistoryWithBLOBs.getProStock())+"");
            }
            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("采购清单导出信息表", 9);
            String[] secondTitles = {"物品名称", "物品类别", "当前库存", "最低采购数量", "最低警示数量", "最高警示数量", "单价", "单位"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
            String[] beanProperty = {"proName", "typeName", "proStock", "shoplistCount", "proLowstock", "proMaxstock", "proPrice", "proUnit"};
            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
            OutputStream out = response.getOutputStream();
            String filename = "采购清单导出信息表.xls";
            filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("content-disposition", "attachment;filename=" + filename);
            workbook.write(out);
            out.close();
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  json;
    }



}
